
********************************************************************************
* Analysis Code for CES Substitutability Experiment, Part I
********************************************************************************

* Paul Musgrave
* rpm47@georgetown.edu
* For Paul Musgrave and Dov Levin

********************************************************************************
* Stata Packages Required (Note)
********************************************************************************

/*

Replication users will need the following packages available from SSC or other
repositories

net install cleanplots, from("https://tdmize.github.io/data/cleanplots") replace

ssc install catplot

ssc install coefplot

ssc install esttab

ssc install mylabels

ssc install grc1leg

ssc install statplot

*/


********************************************************************************
* Stata Cruft
********************************************************************************

clear all

version 18

set more off


set scheme plotplainblind


******** EDIT DIRECTORY STRUCTURE TO FIT YOUR COMPUTER ********

global MyProject "~/Dropbox/0001 Academic Projects/Ongoing/0155 Voting Abroad Purpose/Replication/Upload/"


use "${MyProject}CCES2022processedUPLOAD.dta", clear

* Hegemony

clonevar hegemony_concern = UMA315
lab var hegemony_concern "US Strengthening or Weakening"

* News interest

clonevar newsint_num = newsint
recode newsint_num (4 = 3) (7 = .)
lab def NEWSINT 3 "Now and then / hardly at all", modify


********************************************************************************
* Balance Table
********************************************************************************

recode hispanic (2 = 0)

lab def HISPANIC 0 "Not Hispanic" 1 "Hispanic", modify
	
********************
* Appendix Table A10

dtable age female pid3 college white hispanic 									///
	, by(subs_treatment) nformat(%8.2g)											///
	export("${MyProject}AppendixTableA10.tex", replace tableonly) ///
	title("Hegemony Experiment \label{tab:substitutabilityBalance}")
	
	

********************************************************************************
* Descriptive Plot
********************************************************************************

/// Figure 5 A)

* We begin by creating a catplot showing support for each strategy. This is a
* slightly more involved process than usual.

lab def strat3waylab 0 "Disapprove" 1 "Neither" 2 "Approve"

* have to shorten the names because of Stata stupidity

local strategies contribute concessionbefore coup concessionafter sanctions

* loop to recode the five-way coding to a three-way coding for easier plotting

foreach var of local strategies {
		clonevar subs_`var'_3way = subs_`var' 
		recode subs_`var'_3way (1=0)(2=0)(3=1)(4=2)(5=2)
		lab val subs_`var'_3way strat3waylab
}

catplot 																		///
	, over(subs_contribute_3way)												///
	over(subs_treatment)														///
	asyvars stack percent(subs_treatment) 										///
	ylabel(none, nolabels nogrid)	ytitle("")									///
	legend(ring(2) pos(12) rows(1) size(tiny)) 									///
	blabel(bar,format(%4.1f) box margin(".5 .5 .5 .5")							///
		fcolor(white) color(black) size(3) position(center))					///
	l1title("")  title("{it:Contribute}")										///
	name(gph_contribute_catplot,replace)
	
catplot 	 																	///
	, over(subs_concessionbefore_3way)											///
	over(subs_treatment)														///
	asyvars stack percent(subs_treatment) 										///
	ylabel(none, nolabels nogrid)	ytitle("")									///
	legend(ring(2) pos(12) rows(1) size(tiny)) 									///
	blabel(bar,format(%4.1f) box margin(".5 .5 .5 .5")							///
		fcolor(white) color(black) size(3) position(center)) 					///
	l1title("")  title("{it:Concession Before}")								///
	name(gph_concessionbefore_catplot,replace)
					
catplot 																		///
	, over(subs_coup_3way)														///
	over(subs_treatment)														///
	asyvars stack percent(subs_treatment) 										///
	ylabel(none, nolabels nogrid)	ytitle("")									///
	legend(ring(2) pos(12) rows(1) size(tiny)) 									///
	blabel(bar,format(%4.1f) box margin(".5 .5 .5 .5")							///
		fcolor(white) color(black) size(3) position(center)) 					///
	l1title("")  title("{it:Encourage a Coup}")									///
	name(gph_coup_catplot,replace)

catplot  																		///
	, over(subs_concessionafter_3way)											///
	over(subs_treatment)														///
	asyvars stack percent(subs_treatment) 										///
	ylabel(none, nolabels nogrid)	ytitle("")									///
	legend(ring(2) pos(12) rows(1) size(tiny)) 									///
	blabel(bar,format(%4.1f) box margin(".5 .5 .5 .5")							///
		fcolor(white) color(black) size(3) position(center)) 					///
	l1title("")  title("{it:Concession After}")									///
	name(gph_concessionafter_catplot,replace)

catplot 																		///
	, over(subs_sanctions_3way)													///
	over(subs_treatment)														///
	asyvars stack percent(subs_treatment) 										///
	ylabel(none, nolabels nogrid)	ytitle("")									///
	legend(ring(2) pos(12) rows(1) size(tiny)) 									///
	blabel(bar,format(%4.1f) box margin(".5 .5 .5 .5")							///
		fcolor(white) color(black) size(3) position(center)) 					///
	l1title("")  title("{it:Sanctions}")										///
	name(gph_sanctions_catplot,replace)

***** combine everything and come up with one plot, named "gph_catplot"
					
grc1leg gph_contribute_catplot gph_concessionbefore_catplot gph_coup_catplot	///
		gph_concessionafter_catplot gph_sanctions_catplot						///
		, imargin(0) rows(5) 													///
		title("{bf: A) Evaluations of Alternative Strategies}",size(small))	///
		caption("{it: N = 830}", size(vsmall)) ///
		name(gph_catplot, replace)
		
		
********************************************************************************
* Analyses of Approval of Strategy
********************************************************************************

rename subs_contribute DV_contribute
rename subs_concessionbefore DV_concessionbefore 
rename subs_coup DV_coup
rename subs_concessionafter DV_concessionafter 
rename subs_sanctions DV_sanctions

keep DV_* subs_treatment white faminc_cat age college female covidexposure 	///
		politicalknowledge strongerweaker ///
		pid7 pid3 caseid newsint_num


reshape long DV_, i(caseid) j(strategy) string


* just creating a human-readable version of the treatment variable
encode strategy, gen(strategy_num)
lab def strategy_numlab 1 "Concede After" 2 "Concede Before" 3 "Contribute" 4 "Coup" 5 "Sanctions"
lab val strategy_num strategy_numlab

ologit DV_  b2.subs_treatment i.strategy_num white age college female i.pid7, cluster(caseid)
est sto model_results
local N = e(N)


* running different models

ologit DV_  b2.subs_treatment i.strategy_num white age college female i.pid7, cluster(caseid)
est sto model_results
local N = e(N)
		
coefplot	model_results														///
	, name(g_coefplot_strat2, replace)	base									///
	title("{bf: B) Approval of Strategy}")										///
	subtitle("{it: Ordinal Logistic Coefficients Shown; N = `N' strategy evaluations}", size(small))	///
	keep(*.subs_treatment *.strategy*)											///
	headings(1.subs_treatment="{bf:Objective}"									///
			1.strategy_num="{bf:Strategy}")										///
	xline(0) note("Standard errors clustered by respondent. Full model includes controls for demographics and party ID.", size(vsmall))	
	
	
* generate substantive implications
est restore model_results
margins, atmeans at(subs_treatment=1) dydx(strategy_num) post
est sto model_humanitarian_dydx

est restore model_results
margins, atmeans at(subs_treatment=2) dydx(strategy_num) post
est sto model_leadership_dydx

est restore model_results
margins, atmeans at(subs_treatment=3) dydx(strategy_num) post
est sto model_economic_dydx

* plot the substantive implications

* humanitarian first

mylabels -15(5)20, myscale(@/100) local(myla)
coefplot (model_humanitarian_dydx, keep(2.strategy_num:*._predict) label(Concede Before))	///
	(model_humanitarian_dydx, keep(3.strategy_num:*._predict) label(Contribute))			///
	(model_humanitarian_dydx, keep(4.strategy_num:*._predict) label(Coup))					///
	(model_humanitarian_dydx, keep(5.strategy_num:*._predict) label(Sanctions))				///
	, recast(bar) vertical barw(0.15) noci													///
	xlabel(1 "Strongly Disapprove" 2 "Disapprove" 3 "Neither" 4 "Approve" 5 "Strongly Approve")	///
	title("{bf:Differences in Predicted Outcomes, Humanitarian Condition}", size(medsmall) span) 		///
		subtitle("{it:Base is Concede After}", size(vsmall) span)							///
		ylab(`myla',labsize(vsmall))														///
		name(g_humanitarian_dydx, replace) 													///
		ytitle("Difference between base and treatment", size(vsmall)) 						///
		legend(rows(1) ring(2) pos(12)) yline(0)  

* humanitarian version for main figure (different headline)

mylabels -15(5)20, myscale(@/100) local(myla)
coefplot (model_humanitarian_dydx, keep(2.strategy_num:*._predict) label(Concede Before))	///
	(model_humanitarian_dydx, keep(3.strategy_num:*._predict) label(Contribute))			///
	(model_humanitarian_dydx, keep(4.strategy_num:*._predict) label(Coup))					///
	(model_humanitarian_dydx, keep(5.strategy_num:*._predict) label(Sanctions))				///
	, recast(bar) vertical barw(0.15) noci													///
	xlabel(1 "Strongly Disapprove" 2 "Disapprove" 3 "Neither" 4 "Approve" 5 "Strongly Approve")	///
	title("{bf:C) Differences in Predicted Outcomes, Humanitarian Condition}", size(medsmall) span) ///
		subtitle("{it:Base is Concede After}", size(vsmall) span)							///
		ylab(`myla',labsize(vsmall))														///
		name(g_humanitarian_dydx_chart, replace) 											///
		ytitle("Difference between base and treatment", size(vsmall)) 						///
		legend(rows(1) ring(2) pos(12)) yline(0) 
		
* then leadership
mylabels -15(5)20, myscale(@/100) local(myla)
coefplot (model_leadership_dydx, keep(2.strategy_num:*._predict) label(Concede Before))		///
	(model_leadership_dydx, keep(3.strategy_num:*._predict) label(Contribute))				///
	(model_leadership_dydx, keep(4.strategy_num:*._predict) label(Coup))					///
	(model_leadership_dydx, keep(5.strategy_num:*._predict) label(Sanctions))				///
	, recast(bar) vertical barw(0.15) noci													///
	xlabel(1 "Strongly Disapprove" 2 "Disapprove" 3 "Neither" 4 "Approve" 5 "Strongly Approve")	///
	title("{bf:Differences in Predicted Outcomes, Leadership Condition}", size(medsmall) span) 	///
		subtitle("{it:Base is Concede After}", size(vsmall) span)							///
		ylab(`myla',labsize(vsmall))														///
		name(g_leader_dydx, replace) 														///
		ytitle("Difference between base and treatment", size(vsmall)) 						///
		legend(rows(1) ring(2) pos(12)) yline(0)
		
* then economic	
mylabels -15(5)20, myscale(@/100) local(myla)
coefplot (model_economic_dydx, keep(2.strategy_num:*._predict) label(Concede Before))		///
	(model_economic_dydx, keep(3.strategy_num:*._predict) label(Contribute))				///
	(model_economic_dydx, keep(4.strategy_num:*._predict) label(Coup))						///
	(model_economic_dydx, keep(5.strategy_num:*._predict) label(Sanctions))					///
	, recast(bar) vertical barw(0.15) noci													///
	xlabel(1 "Strongly Disapprove" 2 "Disapprove" 3 "Neither" 4 "Approve" 5 "Strongly Approve")	///
	title("{bf:Differences in Predicted Outcomes, Economic Condition}", size(medsmall) span) 	///
		subtitle("{it:Base is Concede After}", size(vsmall) span)							///
		ylab(`myla',labsize(vsmall))														///
		name(g_econ_dydx, replace) 															///
		ytitle("Difference between base and treatment", size(vsmall)) 						///
		legend(rows(1) ring(2) pos(12)) yline(0)

********************
* Figure 5

gr combine  g_coefplot_strat2 g_humanitarian_dydx_chart, name(g_temp, replace) rows(2)
gr combine gph_catplot g_temp, rows(1)


graph export "${MyProject}00 Main Figure 5.pdf", replace


********************
* Appendix Figure A7

* all three substantive combined
gr combine g_humanitarian_dydx g_leader_dydx g_econ_dydx, name(g_combined, replace) rows(3)

gr display, xsize(6) ysize(9)

gr export "${MyProject}01 Figure A7.pdf", replace


* run additional models for robustness

ologit DV_  b2.subs_treatment i.strategy_num white age college female if pid7 <= 3, cluster(caseid)
est sto model_dem

ologit DV_  b2.subs_treatment i.strategy_num white age college female if pid7 >= 5 & pid7 < 8, cluster(caseid)
est sto model_gop


ologit DV_  b2.subs_treatment i.strategy_num white age  female i.pid7 if college == 0, cluster(caseid)
est sto model_nocol

ologit DV_  b2.subs_treatment i.strategy_num white age  female i.pid7 if college == 1, cluster(caseid)
est sto model_college


ologit DV_  b2.subs_treatment i.strategy_num white age  female i.pid7 college if newsint_num  == 1, cluster(caseid)
est sto model_news1

ologit DV_  b2.subs_treatment i.strategy_num white age  female i.pid7 college if newsint_num  == 2, cluster(caseid)
est sto model_news2


ologit DV_  b2.subs_treatment i.strategy_num white age  female i.pid7 college if newsint_num  == 3, cluster(caseid)
est sto model_news3


********************
* Appendix Table A11

esttab model_results model_dem 	model_gop model_news1 model_news2 model_news3 			///
		using "${MyProject}AppendixTableA11.tex"		///
		, replace nobase label 	longtable  noomitted							///
		drop(*cut*)					///
		mtitles("Base" "Democrats Only" "GOP Only" "News Mostly" "News Some" "News Rarely")		///
		title("Substitutability Experiment Results" "\label{tab:substitutabilitydovreq}")
